package com.property.manage.controller.parking;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.property.manage.model.parking.ParkingRentArrearsDetailVo;
import com.property.manage.model.parking.ParkingRentArrearsVo;
import com.property.manage.model.parking.ParkingRentPayVo;
import com.property.manage.model.parking.ParkingRentVo;
import com.property.manage.model.print.ArrearsPayBillVo;
import com.property.manage.model.print.PayBillVo;
import com.property.manage.service.parking.ParkingRentArrearsService;
import com.property.manage.service.parking.ParkingRentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Api(tags = "车位出租管理")
@RestController
@RequestMapping("/parking/rent")
@AllArgsConstructor
@Validated
public class ParkingRentController {

    private ParkingRentService parkingRentService;

    private ParkingRentArrearsService parkingRentArrearsService;


    @ApiOperation("出租记录")
    @GetMapping("/page")
    public CustomPage<ParkingRentVo> userPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                              @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                              @ApiParam("小区编号") @RequestParam(value = "villageId", required = false) Integer villageId,
                                              @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                              @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                              @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber) {
        return parkingRentService.page(pageNo, pageSize, villageId, building, unit, roomNumber);
    }


    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@ApiParam("楼栋id") @RequestParam(value = "buildingId") @NotNull(message = "请选择楼栋") Integer buildingId,
                    @ApiParam("姓名") @RequestParam(value = "userName", required = false) String userName,
                    @ApiParam("联系方式") @RequestParam(value = "phone", required = false) String phone,
                    @ApiParam("车位个数") @RequestParam(value = "parkingNum", required = false) Integer parkingNum,
                    @ApiParam("车位编号") @RequestParam(value = "parkingNo", required = false) String parkingNo,
                    @ApiParam("租金") @RequestParam(value = "rentFee") @NotNull(message = "请输入每月租金") BigDecimal rentFee,
                    @ApiParam("出租日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                    @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate,
                    @ApiParam("车牌号码") @RequestParam(value = "cardNo", required = false) String cardNo
    ) {
        parkingRentService.add(buildingId, userName, phone, parkingNum, parkingNo, rentFee, startDate, endDate, cardNo);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@ApiParam("租赁编号") @RequestParam(value = "id") @NotNull(message = "租赁编号为空") Integer id,
                       @ApiParam("楼栋id") @RequestParam(value = "buildingId") @NotNull(message = "请选择楼栋") Integer buildingId,
                       @ApiParam("姓名") @RequestParam(value = "userName", required = false) String userName,
                       @ApiParam("联系方式") @RequestParam(value = "phone", required = false) String phone,
                       @ApiParam("车位个数") @RequestParam(value = "parkingNum", required = false) Integer parkingNum,
                       @ApiParam("车位编号") @RequestParam(value = "parkingNo", required = false) String parkingNo,
                       @ApiParam("租金") @RequestParam(value = "rentFee") @NotNull(message = "请输入每月租金") BigDecimal rentFee,
                       @ApiParam("出租日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                       @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate,
                       @ApiParam("车牌号码") @RequestParam(value = "cardNo", required = false) String cardNo
    ) {
        parkingRentService.update(id, buildingId, userName, phone, parkingNum, parkingNo, rentFee, startDate, endDate, cardNo);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public void delete(@ApiParam("租赁编号") @RequestParam(value = "id") @NotNull(message = "租赁编号为空") Integer id) {
        parkingRentService.delete(id);
    }

    @ApiOperation("批量划扣")
    @PostMapping("/deduction")
    public void deduction(@ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId,
                          @ApiParam("小区编号") @RequestParam("villageId") @NotNull(message = "小区编号为空") Integer villageId,
                          @ApiParam("划扣年份") @RequestParam("year") @NotNull(message = "划扣年份为空") Integer year,
                          @ApiParam("划扣月份") @RequestParam("month") @NotNull(message = "划扣月份为空") Integer month) {

        parkingRentService.deduction(userId, villageId, year, month);

    }

    @ApiOperation("缴费记录")
    @GetMapping("/pay/page")
    public CustomPage<ParkingRentPayVo> payPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                                @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                                @ApiParam("小区编号") @RequestParam(value = "villageId", required = false) Integer villageId,
                                                @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                                @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                                @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                @ApiParam("开始日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                                                @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate) {
        return parkingRentService.payPage(pageNo, pageSize, villageId, building, unit, roomNumber, startDate, endDate);
    }

    @ApiOperation("缴费删除")
    @PostMapping("/pay/delete")
    public void deletePay(@ApiParam("缴费编号") @RequestParam("id") @NotNull(message = "缴费编号为空") Integer id) {
        parkingRentService.payDelete(id);
    }

    @ApiOperation("缴费记录导出")
    @GetMapping("/pay/export")
    public void payExport(@ApiParam("小区编号") @RequestParam(value = "villageId", required = false) Integer villageId,
                          @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                          @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                          @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                          @ApiParam("开始日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                          @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate,
                          HttpServletRequest request, HttpServletResponse response) {
        parkingRentService.payExport(villageId, building, unit, roomNumber, startDate, endDate, request, response);
    }

    @ApiOperation("打印票据")
    @GetMapping("/pay/bill")
    public PayBillVo payBill(@ApiParam("支付编号") @RequestParam(value = "id") @NotNull(message = "支付编号为空") Integer id) {

        return parkingRentService.payBill(id);
    }

    @ApiOperation("欠费记录")
    @GetMapping("/arrears/page")
    public CustomPage<ParkingRentArrearsVo> arrearsPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                                        @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                                        @ApiParam("小区编号") @RequestParam(value = "villageId", required = false) Integer villageId,
                                                        @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                                        @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                                        @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                        @ApiParam("开始日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                                                        @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate) {
        return parkingRentArrearsService.arrearsPage(pageNo, pageSize, villageId, building, unit, roomNumber, startDate, endDate);
    }


    @ApiOperation("欠费明细列表")
    @GetMapping("/arrears/detail/list")
    public List<ParkingRentArrearsDetailVo> arrearsDetailList(@ApiParam("楼栋编号") @RequestParam(value = "buildingId") @NotNull(message = "楼栋编号为空") Integer buildingId) {

        return parkingRentArrearsService.arrearsDetailList(buildingId);
    }

    @ApiOperation("欠费明细导出")
    @GetMapping("/arrears/detail/export")
    public void arrearsDetailList(@ApiParam("小区编号") @RequestParam(value = "villageId", required = false) Integer villageId,
                                  @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                  @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                  @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                  @ApiParam("开始日期") @RequestParam(value = "startDate", required = false) LocalDate startDate,
                                  @ApiParam("结束日期") @RequestParam(value = "endDate", required = false) LocalDate endDate,
                                  HttpServletRequest request, HttpServletResponse response) {

        parkingRentArrearsService.arrearsDetailExport(villageId, building, unit, roomNumber, startDate, endDate, request, response);
    }

    @ApiOperation("欠费收取")
    @PostMapping("/arrears/charge")
    public ArrearsPayBillVo charge(@ApiIgnore("用户编号") @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId,
                                   @ApiParam("楼栋编号") @RequestParam("buildingId") @NotNull(message = "楼栋编号为空") Integer buildingId,
                                   @ApiParam("欠费编号（转换成json数据）") @RequestParam("arrearsIds") @NotBlank(message = "欠费编号为空") String arrearsIds,
                                   @ApiParam("合计金额") @RequestParam("totalMoney") @NotNull(message = "合计金额为空") BigDecimal totalMoney,
                                   @ApiParam("收款金额") @RequestParam("chargeMoney") @NotNull(message = "收款金额") BigDecimal chargeMoney,
                                   @ApiParam("支付方式") @RequestParam("payType") @NotBlank(message = "支付方式") String payType,
                                   @ApiParam("使用预存") @RequestParam(value = "useStorage", required = false, defaultValue = "false") Boolean useStorage,
                                   @ApiParam("零钱转存") @RequestParam(value = "storageReturn", required = false, defaultValue = "false") Boolean storageReturn,
                                   @ApiParam("是否打印") @RequestParam(value = "print", required = false, defaultValue = "false") Boolean print,
                                   @ApiParam("备注") @RequestParam(value = "remark", required = false) String remark) {

        return parkingRentArrearsService.charge(userId, buildingId, arrearsIds, totalMoney, chargeMoney, payType, useStorage, storageReturn, print, remark);
    }
}
